package net.zjitc.io.bufferedstream;

import java.io.*;
import java.util.Scanner;

/**
 * @Author admin
 * @Date 2019/4/10 9:07
 */
public class test {
    public static void main(String[] args) {
        System.out.println("请输入内容：");
        Scanner scanner=new Scanner(System.in);
        String s=scanner.nextLine();
        /** 通过字节缓冲流写入文件
         * 1、创建缓冲流对象
         * 2、调用流对象的write方法
         * */
        OutputStream out= null;
        BufferedOutputStream bos=null;
        InputStream in=null;
        BufferedInputStream bis=null;
        try {
            /*写入*/
            out = new FileOutputStream("1.txt");
            bos =new  BufferedOutputStream(out);
            byte[] b=s.getBytes();
            bos.write(b);
            /*刷新此缓冲的输出流。这迫使所有缓冲的输出字节被写出到底层输出流中。*/
            bos.flush();
            /*读取*/
            in=new FileInputStream("1.txt");
            bis=new BufferedInputStream(in);
            int len=0;
            /*while ((len=bis.read())!=-1){   //数字

                System.out.println(len);
            }*/
            byte[] bytes=new byte[1024];
            while ((len=bis.read(bytes))!=-1){   //字符串
                System.out.println(new String (bytes,0,len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
                out.close();
                bis.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
